﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;

namespace Urb.Weather.Ticker.UI.Converters
{
    public class NullableNumberConverter<T> : IValueConverter where T : struct
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || !(value is Nullable<T>))
                return string.Empty;

            var number = (Nullable<T>)value;

            if (!number.HasValue)
                return "-";

            return number.Value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    
    [ValueConversion(typeof(decimal?), typeof(string))]
    public class NullableDecimalConverter : NullableNumberConverter<decimal>
    {
    }    
    
    [ValueConversion(typeof(short?), typeof(string))]
    public class NullableShortConverter : NullableNumberConverter<short>
    {
    }
}
